/**
 * 
 */
package com.wegax.finprod.accountcard;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.TextField;
import com.wegax.finprod.message.MessageCodes;
import com.wegax.finprod.message.Messages;
import com.wegax.finprod.wizardform.WizardForm;
import com.wegax.finprod.wizardform.WizardFormStep;

/**
 * @author wegax
 * 
 */
class AddressInformationWizardStep extends FormLayout implements WizardFormStep<PersonalAccountDebitCardMapper> {

	private static final Logger log = LoggerFactory.getLogger(AddressInformationWizardStep.class);

	private WizardForm<PersonalAccountDebitCardMapper> wizardForm;

	private Form form;

	private TextField email;

	private ComboBox country1;

	private TextField city1;

	private TextField street1;

	private TextField houseNr1;

	private TextField flatNr1;

	private TextField postalCode1;

	private OptionGroup correspondendAddressAvailability;

	private ComboBox country2;

	private TextField city2;

	private TextField street2;

	private TextField houseNr2;

	private TextField flatNr2;

	private TextField postalCode2;

	public AddressInformationWizardStep() {
		initComponents();
	}

	private void initComponents() {
		form = new Form();
		addComponent(form);

		email = new TextField(Messages.getString(MessageCodes.EMAIL_FIELD_CAPTION));
		email.setRequired(true);
		email.setRequiredError(Messages.getString(MessageCodes.EMAIL_FIELD_REQUIRED_MESSAGE));
		email.addValidator(new EmailValidator(Messages.getString(MessageCodes.EMAIL_FIELD_INCORRECT)));
		form.addField(EMAIL_FIELD, email);

		country1 = new ComboBox(Messages.getString(MessageCodes.COUNTRY_FIELD_CAPTION));
		for (CountryCode countryCode : CountryCode.values()) {
			country1.addItem(countryCode);
			country1.setItemCaption(countryCode, Messages.getString(MessageCodes.COUNTRY_CODE_CAPTION + countryCode));
		}
		country1.select(CountryCode.POLAND);
		country1.setEnabled(false);
		form.addField(COUNTRY1_FIELD, country1);

		city1 = new TextField(Messages.getString(MessageCodes.CITY_FIELD_CAPTION));
		city1.setRequired(true);
		city1.setRequiredError(Messages.getString(MessageCodes.CITY_FIELD_REQUIRED_MESSAGE));
		form.addField(CITY1_FIELD, city1);

		street1 = new TextField(Messages.getString(MessageCodes.STREET_FIELD_CAPTION));
		street1.setRequired(true);
		street1.setRequiredError(Messages.getString(MessageCodes.STREET_FIELD_REQUIRED_MESSAGE));
		form.addField(STREET1_FIELD, street1);

		houseNr1 = new TextField(Messages.getString(MessageCodes.HOUSENR_FIELD_CAPTION));
		houseNr1.setRequired(true);
		houseNr1.setRequiredError(Messages.getString(MessageCodes.HOUSENR_FIELD_REQUIRED_MESSAGE));
		form.addField(HOUSENR1_FIELD, houseNr1);

		flatNr1 = new TextField(Messages.getString(MessageCodes.FLATNR_FIELD_CAPTION));
		form.addField(FLATNR1_FIELD, flatNr1);

		postalCode1 = new TextField(Messages.getString(MessageCodes.POSTALCODE_FIELD_CAPTION));
		postalCode1.setRequired(true);
		postalCode1.setRequiredError(Messages.getString(MessageCodes.POSTALCODE_FIELD_REQUIRED_MESSAGE));
		postalCode1.addValidator(new RegexpValidator("[0-9]{2}-[0-9]{3}", Messages
				.getString(MessageCodes.POSTALCODE_FIELD_INCORRECT)));
		form.addField(POSTALCODE1_FIELD, postalCode1);

		correspondendAddressAvailability = new OptionGroup("", Arrays.asList(Messages
				.getString(MessageCodes.ADDRESS_CORRESPONDENCE_CAPTION)));
		correspondendAddressAvailability.setMultiSelect(true);
		correspondendAddressAvailability.setImmediate(true);
		correspondendAddressAvailability.addListener(new Property.ValueChangeListener() {

			@Override
			public void valueChange(ValueChangeEvent event) {
				Set<?> values = (Set<?>) event.getProperty().getValue();
				if (!values.isEmpty()) {
					correspondencAddressVisible(true);
					correspondenceAddressValidation(true);
				} else {
					correspondencAddressVisible(false);
					correspondenceAddressValidation(false);
				}
			}
		});
		form.addField(CORRESPONDANCE_AVAILABILITY_FIELD, correspondendAddressAvailability);

		country2 = new ComboBox(Messages.getString(MessageCodes.COUNTRY_FIELD_CAPTION));
		for (CountryCode countryCode : CountryCode.values()) {
			country2.addItem(countryCode);
			country2.setItemCaption(countryCode, Messages.getString(MessageCodes.COUNTRY_CODE_CAPTION + countryCode));
		}
		country2.setEnabled(false);
		form.addField(COUNTRY2_FIELD, country2);

		city2 = new TextField(Messages.getString(MessageCodes.CITY_FIELD_CAPTION));
		city2.setRequired(true);
		city2.setRequiredError(Messages.getString(MessageCodes.CITY_FIELD_REQUIRED_MESSAGE));
		city2.setEnabled(true);
		form.addField(CITY2_FIELD, city2);

		street2 = new TextField(Messages.getString(MessageCodes.STREET_FIELD_CAPTION));
		street2.setRequired(true);
		street2.setRequiredError(Messages.getString(MessageCodes.STREET_FIELD_REQUIRED_MESSAGE));
		form.addField(STREET2_FIELD, street2);

		houseNr2 = new TextField(Messages.getString(MessageCodes.HOUSENR_FIELD_CAPTION));
		houseNr2.setRequired(true);
		houseNr2.setRequiredError(Messages.getString(MessageCodes.HOUSENR_FIELD_REQUIRED_MESSAGE));
		form.addField(HOUSENR2_FIELD, houseNr2);

		flatNr2 = new TextField(Messages.getString(MessageCodes.FLATNR_FIELD_CAPTION));
		form.addField(FLATNR2_FIELD, flatNr2);

		postalCode2 = new TextField(Messages.getString(MessageCodes.POSTALCODE_FIELD_CAPTION));
		postalCode2.setRequired(true);
		postalCode2.setRequiredError(Messages.getString(MessageCodes.POSTALCODE_FIELD_REQUIRED_MESSAGE));
		postalCode2.addValidator(new RegexpValidator("[0-9]{2}-[0-9]{3}", Messages
				.getString(MessageCodes.POSTALCODE_FIELD_INCORRECT)));
		form.addField(POSTALCODE2_FIELD, postalCode2);

		correspondencAddressVisible(false);
		correspondenceAddressValidation(false);

		setDefaultValues();
	}

	private void setDefaultValues() {
		email.setValue("john.smith@domain.com");
		city1.setValue("Poznań");
		street1.setValue("Bukowska");
		houseNr1.setValue("46A");
		flatNr1.setValue("7");
		postalCode1.setValue("60-111");
	}

	private static final String EMAIL_FIELD = "email";

	private static final String COUNTRY1_FIELD = "country1";

	private static final String CITY1_FIELD = "city1";

	private static final String STREET1_FIELD = "street1";

	private static final String HOUSENR1_FIELD = "houseNr1";

	private static final String FLATNR1_FIELD = "flatNr1";

	private static final String POSTALCODE1_FIELD = "postalCode1";

	private static final String CORRESPONDANCE_AVAILABILITY_FIELD = "correspondanceAvailability";

	private static final String COUNTRY2_FIELD = "country2";

	private static final String CITY2_FIELD = "city2";

	private static final String STREET2_FIELD = "street2";

	private static final String HOUSENR2_FIELD = "houseNr2";

	private static final String FLATNR2_FIELD = "flatNr2";

	private static final String POSTALCODE2_FIELD = "postalCode2";

	private void correspondenceAddressValidation(boolean isValidable) {
		form.getField(CITY2_FIELD).setRequired(isValidable);
		form.getField(STREET2_FIELD).setRequired(isValidable);
		form.getField(HOUSENR2_FIELD).setRequired(isValidable);
		form.getField(POSTALCODE2_FIELD).setRequired(isValidable);
	}

	private void correspondencAddressVisible(boolean isVisible) {
		form.getField(COUNTRY2_FIELD).setVisible(isVisible);
		form.getField(CITY2_FIELD).setVisible(isVisible);
		form.getField(STREET2_FIELD).setVisible(isVisible);
		form.getField(HOUSENR2_FIELD).setVisible(isVisible);
		form.getField(FLATNR2_FIELD).setVisible(isVisible);
		form.getField(POSTALCODE2_FIELD).setVisible(isVisible);

		if (isVisible) {
			country2.select(CountryCode.POLAND);
		} else {
			country2.select(country2.getNullSelectionItemId());
			form.getField(CITY2_FIELD).setValue("");
			form.getField(STREET2_FIELD).setValue("");
			form.getField(HOUSENR2_FIELD).setValue("");
			form.getField(FLATNR2_FIELD).setValue("");
			form.getField(POSTALCODE2_FIELD).setValue("");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#getCaption()
	 */
	@Override
	public String getCaption() {
		return Messages.getString(MessageCodes.ADDRESS_INFORMATION_TITLE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#getContent()
	 */
	@Override
	public Component getContent() {
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#onAdvance()
	 */
	@Override
	public boolean onAdvance() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#onBack()
	 */
	@Override
	public boolean onBack() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.wizardform.WizardFormStep#getForm()
	 */
	@Override
	public Form getForm() {
		return form;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.wizardform.WizardFormStep#save(java.lang.Object)
	 */
	@Override
	public void map(PersonalAccountDebitCardMapper bean) {
		bean.setEmail((String) email.getValue());
		bean.setCountry1((CountryCode) country1.getValue());
		bean.setCity1((String) city1.getValue());
		bean.setStreet1((String) street1.getValue());
		bean.setHouseNr1((String) houseNr1.getValue());
		bean.setFlatNr1((String) flatNr1.getValue());
		bean.setPostalCode1((String) postalCode1.getValue());
		if (!((Collection<?>) correspondendAddressAvailability.getValue()).isEmpty()) {
			bean.setCorrespondencAdrressAvailable(true);
			bean.setCountry2((CountryCode) country2.getValue());
			bean.setCity2((String) city2.getValue());
			bean.setStreet2((String) street2.getValue());
			bean.setHouseNr2((String) houseNr2.getValue());
			bean.setFlatNr2((String) flatNr2.getValue());
			bean.setPostalCode2((String) postalCode2.getValue());
		} else {
			bean.setCorrespondencAdrressAvailable(false);
			bean.setCountry2(null);
			bean.setCity2(null);
			bean.setStreet2(null);
			bean.setHouseNr2(null);
			bean.setFlatNr2(null);
			bean.setPostalCode2(null);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.wegax.finprod.wizardform.WizardFormStep#initFormStep(com.wegax.finprod.wizardform.WizardForm)
	 */
	@Override
	public void initFormStep(WizardForm<PersonalAccountDebitCardMapper> wizardForm) {
		this.wizardForm = wizardForm;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.wizardform.WizardFormStep#onShow()
	 */
	@Override
	public void onShow() {

	}

}
